package com.xci.sys.service;

import com.xci.core.annotation.CurrentUser;
import com.xci.core.annotation.Log;
import com.xci.core.annotation.Valid;
import com.xci.core.base.BaseService;
import com.xci.core.base.RestMessage;
import com.xci.core.helper.Helper;
import com.xci.core.internal.Const;
import com.xci.core.internal.JsonListData;
import com.xci.sys.entity.SysReport;
import com.xci.sys.filter.ParamFilter;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.function.Predicate;

/**
 * 报表服务
 */
@Service
public class ReportService extends BaseService {
    @Resource
    private JsonListData<SysReport> reportJsonList;

    /**
     * 新建报表
     *
     * @param entity 报表对象
     */
    @Log(module = Const.Module.Report, msg = "新建报表")
    public RestMessage insert(@Valid SysReport entity) {
        return save(entity,true);
    }

    /**
     * 修改报表
     *
     * @param entity 报表对象
     */
    @Log(module = Const.Module.Report, msg = "修改报表")
    public RestMessage update(@Valid SysReport entity) {
        return save(entity,false);
    }

    /**
     * 删除报表
     *
     * @param codes 报表编码字符串,多个逗号隔开
     */
    @Log(module = Const.Module.Report, msg = "删除报表")
    public RestMessage delete(@NotBlank(message = "请指定报表编码字符串") String codes) {
        reportJsonList.remove(data -> {
            for (String code : Helper.splitToArray(codes)) {
                data.removeIf(p -> p.getCode().equalsIgnoreCase(code));
            }
        });
        return RestMessage.success();
    }

    /**
     * 检查报表编码唯一性
     *
     * @param code 报表编码
     * @return 如果存在返回true, 否则返回false
     */
    public Boolean existByCode(@NotBlank(message = "请指定报表编码") String code) {
        return reportJsonList.exist(data->data.stream().anyMatch(p -> p.getCode().equalsIgnoreCase(code)));
    }

    /**
     * 根据编码查询单个报表
     *
     * @param code 报表编码
     * @return 返回报表对象
     */
    public SysReport selectByCode(@NotBlank(message = "请指定报表编码") String code) {
        return reportJsonList.get(p -> p.getCode().equalsIgnoreCase(code));
    }

    /**
     * 查询报表列表
     *
     * @return 返回报表对象列表
     */
    public List<SysReport> selectList() {
        return reportJsonList.getList(null);
    }

    /**
     * 刷新报表缓存
     */
    public void refreshCache() {
        reportJsonList.reload();
    }

    /**
     * 保存报表
     * @param entity 报表对象
     * @param created 是否新建
     */
    private RestMessage save(SysReport entity, boolean created) {
        Predicate<SysReport> predicate = p -> p.getCode().equalsIgnoreCase(entity.getCode());
        //新增时编码必须唯一,修改时新的编码数量不能超过1
        var has = reportJsonList.exist(data->
                (created && data.stream().anyMatch(predicate)) || (data.stream().filter(predicate).count() > 1));
        if (has) {
            return RestMessage.fail(Helper.format("报表编码[{}]已经存在", entity.getCode()));
        }

        if(created) {// 新建
            reportJsonList.add(entity);
        } else {     //修改
            reportJsonList.update(entity,p -> p.getCode().equalsIgnoreCase(entity.getCode()));
        }
        return RestMessage.success();
    }
}